﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Srvtools;

public partial class WorkTime_ViewWorkOn4Lunch : System.Web.UI.Page
{
    private Srvtools.WebDataSet wsViewDepWorkOnCheck;
    private Hashtable EmpWorkOnDateTable;
    private Hashtable CompWorkDateDefine;

    private string StartWorkOn;
    private string EndWorkOn;
    private string LateOfStartTime;
    private string LeaveOfEndTime;

    private DateTime FirstDayOfThisMonth;
    private DateTime LastDayOfThisMonth;
    private string StartDateKey = "";
    private string EndDataKey = "";

    private void InitializeComponent()
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(WorkTime_ViewWorkOn4Lunch));
        this.wsViewDepWorkOnCheck = new Srvtools.WebDataSet();
        ((System.ComponentModel.ISupportInitialize)(this.wsViewDepWorkOnCheck)).BeginInit();
        // 
        // wsViewDepWorkOnCheck
        // 
        this.wsViewDepWorkOnCheck.Active = false;
        this.wsViewDepWorkOnCheck.AlwaysClose = false;
        this.wsViewDepWorkOnCheck.DeleteIncomplete = true;
        this.wsViewDepWorkOnCheck.Guid = "5abc890f-29be-408a-bb92-77dcb8bb9319";
        this.wsViewDepWorkOnCheck.LastKeyValues = null;
        this.wsViewDepWorkOnCheck.Locale = new System.Globalization.CultureInfo("zh-CN");
        this.wsViewDepWorkOnCheck.PacketRecords = 100;
        this.wsViewDepWorkOnCheck.Position = -1;
        this.wsViewDepWorkOnCheck.RemoteName = "SWorkTime.View_WorkTime_Overtime";
        this.wsViewDepWorkOnCheck.ServerModify = false;
        ((System.ComponentModel.ISupportInitialize)(this.wsViewDepWorkOnCheck)).EndInit();

    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            int year = DateTime.Now.Year;
            int Month = DateTime.Now.Month;

            DlYear.SelectedValue = year.ToString();
            DLMonth.SelectedValue = Month.ToString();

            MasterPage_MasterPage mpg = (MasterPage_MasterPage)this.Page.Master;
            mpg.HelpHref = "~/Help/WorkTime/查员工午饭津贴的考勤数据.htm";
            mpg.PageName = "查员工午饭津贴的考勤数据";
            mpg.PicUrl = "";
            mpg.IntiBar();
        }
    }

    protected void MakeDataSet(String SelDepId)
    {
        InitializeComponent();

        //get the WorkOn data
        DataSet EmpList;
        string sSql;

        sSql = " SELECT Employees.EmployeeID, Employees.EmpName, Department.DepartmentName ";
        sSql = sSql + "FROM  Employees INNER JOIN Department ON Employees.DepartmentId = Department.DepartmentId ";
        sSql = sSql + string.Format("WHERE (Employees.EmpStatus = 1) AND (Employees.DepartmentId LIKE '{0}%') ", SelDepId);
        sSql = sSql + " ORDER BY Employees.DepartmentId ";
        EmpList = wsViewDepWorkOnCheck.Execute(sSql);


        DataTable svTable;
        svTable = MakeNewWTable();
        DataRow SR;

        string EmpId = "";
        string EmpName = "";
        string Daykey = "";
        string DepartmentName = "";

        for (int i = 0; i < EmpList.Tables[0].Rows.Count; i++)
        {

            EmpId = (string)EmpList.Tables[0].Rows[i]["EmployeeID"].ToString().Trim();
            EmpName = (string)EmpList.Tables[0].Rows[i]["EmpName"];
            DepartmentName = (string)EmpList.Tables[0].Rows[i]["DepartmentName"];


            DateTime sDate = FirstDayOfThisMonth;
            DateTime eDate = LastDayOfThisMonth;

            int WorkOnDay = 0;
            int WorkOnDayAF = 0;


            while (sDate <= eDate)
            {
                Daykey = sDate.Year.ToString().Trim() + "-" + sDate.Month.ToString().Trim() + "-" + sDate.Day.ToString().Trim();
                string DKey = EmpId + "-" + Daykey;
                Single DayCC;
                bool NowIsWorkingDay = IsWorkingDay(sDate, out  DayCC);

                //Single OutHour = 0;

                if (NowIsWorkingDay == false) //非工作日
                {
                    //
                }
                else  //工作日
                {
                    if (EmpWorkOnDateTable.ContainsKey(DKey))
                    {
                        DateTime[] DtIn = (DateTime[])EmpWorkOnDateTable[DKey];
                        DateTime DateV = DateTime.Parse(Daykey + " " + "12:45");

                        if (DateV < DtIn[0]) //下午到
                        {
                            WorkOnDayAF = WorkOnDayAF + 1;
                        }
                        else
                        {
                            WorkOnDay = WorkOnDay + 1;
                        }

                    }
                    else
                    {
                        // 无考勤记录数据!
                    }
                }

                sDate = sDate.AddDays(1);
            }


            SR = svTable.NewRow();

            SR["EmployeeID"] = EmpId;
            SR["EmpName"] = EmpName;
            SR["DepartmentName"] = DepartmentName;

            SR["Month"] = DLMonth.SelectedValue + "月";   //sDate.Month.ToString() + "月";
            SR["WorkOnDay"] = WorkOnDay;
            SR["WorkOnDayAF"] = WorkOnDayAF;

            svTable.Rows.Add(SR);

        }

        wgvMaster.DataSource = svTable;
        wgvMaster.DataBind();

    }


    private DataTable MakeNewWTable()
    {
        DataTable WTable = new DataTable("EmpYearTable");

        {
            WTable.Columns.Add(new DataColumn("EmployeeID", typeof(Int32)));
            WTable.Columns.Add(new DataColumn("EmpName", typeof(string)));
            WTable.Columns.Add(new DataColumn("DepartmentName", typeof(string)));

            WTable.Columns.Add(new DataColumn("Month", typeof(string)));
            WTable.Columns.Add(new DataColumn("WorkOnDay", typeof(Int32))); //正常天数
            WTable.Columns.Add(new DataColumn("WorkOnDayAF", typeof(Int32)));//下午后到天数

        }
        return WTable;
    }


    protected void Refresh_Click(object sender, EventArgs e)
    {
        string SelDepId = DepartmentDList1.SelectDepmanetId;
        if (SelDepId.Length > 0 && SelDepId != "0")
        {
            int year = int.Parse(DlYear.SelectedValue);
            int month = int.Parse(DLMonth.SelectedValue);
            FirstDayOfThisMonth = new DateTime(year, month, 1);
            LastDayOfThisMonth = new DateTime(year, month, DateTime.DaysInMonth(year, month));


            StartDateKey = FirstDayOfThisMonth.Year.ToString().Trim() + "-" + FirstDayOfThisMonth.Month.ToString().Trim() + "-" + FirstDayOfThisMonth.Day.ToString().Trim();
            EndDataKey = LastDayOfThisMonth.Year.ToString().Trim() + "-" + LastDayOfThisMonth.Month.ToString().Trim() + "-" + LastDayOfThisMonth.Day.ToString().Trim();

            DoWorkOnInit(StartDateKey, EndDataKey);
            GetData(SelDepId, StartDateKey, EndDataKey);

            MakeDataSet(SelDepId);
        }
        else
        {
            string message = "请首先选择部门!";
            // this.Page.Response.Write("<script>alert(\"" + message + "\");</script>");
            ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert(\"" + message + "\");", true);
        }

    }

    #region 工作时间分析

    /// <summary>
    /// 设置员工分析初试化数据
    /// </summary>
    /// <param name="DateS">本月起始日</param>
    /// <param name="DataE">本月终止日</param>
    private void DoWorkOnInit(string DateS, string DataE)
    {

        StartWorkOn = ConfigurationManager.AppSettings["StartTimeOfWorkOnTime"];
        EndWorkOn = ConfigurationManager.AppSettings["EndTimeOfWorkOnTime"];
        LateOfStartTime = ConfigurationManager.AppSettings["LateOfStartTime"];
        LeaveOfEndTime = ConfigurationManager.AppSettings["LeaveOfEndTime"];

        //取得公司工作日期定义的数据集合
        object[] objParam2 = new object[2];
        objParam2[0] = DateS;
        objParam2[1] = DataE;
        object[] myRet2 = CliUtils.CallMethod("SWorkTime", "GetCompWorkDateDefine", objParam2);
        if (myRet2 != null && (int)myRet2[0] == 0)
        {
            CompWorkDateDefine = (Hashtable)myRet2[1];
        }



    }


    /// <summary>
    /// 取得本月某员工的的考勤和公司日期定义日的集合
    /// </summary>
    /// <param name="EmpId">员工工号</param>
    /// <param name="DateS">本月起始日</param>
    /// <param name="DataE">本月终止日</param>
    private void GetData(string depId, string DateS, string DataE)
    {
        object[] objParam = new object[3];
        objParam[0] = depId;
        objParam[1] = DateS;
        objParam[2] = DataE;
        object[] myRet = CliUtils.CallMethod("SWorkTime", "GetEmpWorkOnDateTableByDeparment", objParam);
        if (myRet != null && (int)myRet[0] == 0)
        {
            EmpWorkOnDateTable = (Hashtable)myRet[1];
        }
    }


    /// <summary>
    /// 判断是否为工作日
    /// </summary>
    /// <param name="Inday"></param>
    /// <returns></returns>
    private bool IsWorkingDay(DateTime Inday, out Single DayCC)
    {
        int DatePeptry;
        string DKey = Inday.Year.ToString().Trim() + "-" + Inday.Month.ToString().Trim() + "-" + Inday.Day.ToString().Trim();
        DayCC = 1.5f;

        if (Inday.DayOfWeek == DayOfWeek.Saturday || Inday.DayOfWeek == DayOfWeek.Sunday)
        {
            if (CompWorkDateDefine.ContainsKey(DKey) == false)
            {
                DatePeptry = 0;
                DayCC = 2.0f;// = "双休日";
                return false;
            }
            else
            {
                object[] DtIn = (object[])CompWorkDateDefine[DKey];
                bool Dbk = (bool)DtIn[0];
                DatePeptry = (int)DtIn[1];

                switch (DatePeptry)
                {
                    case 1:
                        DayCC = 1.5f;// "工作日";
                        break;
                    case 2:
                        DayCC = 3.0f;// "法定假日";
                        break;
                    case 3:
                        DayCC = 1.5f;//;
                        break;
                    default:
                        DayCC = 1.5f;//;
                        break;
                }

                return Dbk;
            }
        }
        else
        {
            if (CompWorkDateDefine.ContainsKey(DKey) == false)
            {
                DatePeptry = 0;
                DayCC = 1.5f;// "工作日";
                return true;
            }
            else
            {
                object[] DtIn = (object[])CompWorkDateDefine[DKey];
                bool Dbk = (bool)DtIn[0];
                DatePeptry = (int)DtIn[1];
                switch (DatePeptry)
                {
                    case 1:
                        DayCC = 1.5f;// "工作日";
                        break;
                    case 2:
                        DayCC = 3.0f;// "法定假日";
                        break;
                    case 3:
                        DayCC = 2.0f;// "休息日";
                        break;
                    default:
                        DayCC = 1.5f;//;
                        break;
                }
                return Dbk;
            }
        }

    }
    #endregion
}
